package com.dongjiantong.web.boss.handler;

import com.alibaba.dubbo.config.annotation.Reference;
import com.dongjiantong.common.exceptions.HttpCode;
import com.dongjiantong.common.exceptions.OPServerException;
import com.dongjiantong.common.redis.RedisUtil;
import com.dongjiantong.common.utils.BeanMapper;
import com.dongjiantong.common.utils.StringUtil;
import com.dongjiantong.common.web.model.BaseLoginUser;
import com.dongjiantong.common.web.sso.LoginInfo;
import com.dongjiantong.common.web.util.UserUtil;
import com.dongjiantong.openplatform.annotations.OpenParamHandler;
import com.dongjiantong.openplatform.core.ParamPreHandle;
import com.dongjiantong.openplatform.exceptions.OPClientException;
import com.dongjiantong.user.model.MzUser;
import com.dongjiantong.user.service.MzUserService;
import com.google.common.collect.Maps;
import lombok.extern.slf4j.Slf4j;

import java.util.Map;

/**
 * @author cao
 * @create 2017-12-15 上午11:28
 **/
@OpenParamHandler(patterns = "*", exclusions = {

        "boss.user.login"
})
@Slf4j
public class SessionHandle implements ParamPreHandle {

    @Reference(version = "1.0.0")
    public MzUserService mzUserService;

    private MzUser queryUser(long userId)
    {
        String key = "mzuser:"+userId;
        MzUser mzUser = RedisUtil.getObject(key,MzUser.class);

        if(mzUser != null)
        {
            return  mzUser;
        }
        else
        {
            mzUser=  mzUserService.getMzUser(userId);
            if(mzUser!=null)
            {
                RedisUtil.setObject(key,mzUser);
            }
        }

        return mzUser;
    }

    @Override
    public void handle(Map<String, Object> params) throws OPServerException {
        if (params.get("sid") == null || StringUtil.isEmpty((String) params.get("sid"))) {
            throw new OPServerException(HttpCode.BAD_REQUEST, "参数不正确(sid)");
        }
        String sessionId = params.get("sid").toString();
        String key=SessionsModel.TOKEN_PREFIX+":"+sessionId;
        Map<String, Object> snapshot= Maps.newHashMap();

            snapshot = RedisUtil.getMaps(key);
            if (snapshot == null || snapshot.size() == 0 || snapshot.get(SessionsModel.USER_ID) == null) {
                throw new OPServerException(HttpCode.BAD_REQUEST, "登录已经过期，请重新登录");
            }

//        // refresh
//        sessionManager.refreshExpireTime(Sessions.TOKEN_PREFIX, sessionId, SessionsModel.LONG_INACTIVE_INTERVAL);
        Long uid = Long.parseLong(snapshot.get(SessionsModel.USER_ID).toString());
        MzUser mzUser = queryUser(uid);
        if (mzUser==null) {
            log.error("fail to find user by id={},cause:{}",
                    uid, "用户不存在");
            throw new OPServerException(HttpCode.BAD_REQUEST,"用户名不存在");
        }
        LoginInfo loginUser = (LoginInfo) wrapLoginUser(mzUser);
        UserUtil.putCurrentUser(loginUser);
    }
    private BaseLoginUser wrapLoginUser(MzUser user) {
       // BaseLoginUser baseLoginUser = LoginUserMaker.from(user);
        LoginInfo loginUser = BeanMapper.map(user, LoginInfo.class);


        return loginUser;
    }
}
